package cn.exrick.xboot.base.serviceimpl

import cn.exrick.xboot.base.entity.AccidentCase
import cn.exrick.xboot.base.entity.Notice
import cn.exrick.xboot.base.mapper.NoticeMapper
import cn.exrick.xboot.base.service.INoticeService
import cn.exrick.xboot.base.vo.AccidentCaseVo
import cn.exrick.xboot.base.vo.NoticeVo
import cn.exrick.xboot.core.common.utils.PageUtil
import cn.exrick.xboot.core.common.utils.ResultUtil
import cn.exrick.xboot.core.common.utils.SecurityUtil
import cn.exrick.xboot.core.common.utils.ToolUtil
import cn.exrick.xboot.core.common.vo.PageVo
import cn.exrick.xboot.core.common.vo.Result
import com.baomidou.mybatisplus.extension.kotlin.KtQueryWrapper
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl
import com.google.common.collect.Maps
import lombok.extern.slf4j.Slf4j
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional

/**
 * 公告接口实现
 * @author Fei
 */
@Slf4j
@Service
@Transactional
open class INoticeServiceImpl : ServiceImpl<NoticeMapper?, Notice?>(), INoticeService {


    @Autowired
    private lateinit var securityUtil: SecurityUtil

    override fun createOrUpdateNotice(notice: Notice): Result<Any?>? {

        return try {

            notice.areaId = securityUtil.currUser.areaId
            saveOrUpdate(notice)
            ResultUtil.success("操作成功")
        }catch (e: Exception) {

            ResultUtil.error(e.message)
        }
    }

    override fun removeNotice(ids: Array<out String>): Result<Any?>? {

        return try {

            removeByIds(ids.toList())
            ResultUtil.success("移除成功")
        }catch (e: Exception) {

            ResultUtil.error(e.message)
        }
    }

    override fun getNoticeDetail(id: String): Result<Notice>? {

        return ResultUtil.data(this.getById(id))
    }

    override fun getNoticeList(pageVo: PageVo, type: Int?): Result<MutableMap<String, Any?>>? {

        return ToolUtil.setListToNul(list(KtQueryWrapper(Notice::class.java)
                .eq(ToolUtil.isNotEmpty(type), Notice::type, type)
                .eq(Notice::areaId, securityUtil.currUser.areaId)
                .orderByDesc(Notice::createTime)))?.run {

            val result:MutableMap<String, Any?> = Maps.newHashMap()
            result["size"] = size
            result["content"] = PageUtil.listToPage(pageVo, mapNotNull { NoticeVo().apply { ToolUtil.copyProperties(it, this)

                typeStr = if (it?.type == 1) "曝光" else "公告"
            } }.toMutableList())

            ResultUtil.data(result, "获取数据成功！")
        } ?: ResultUtil.error(201, "暂无数据")
    }
}